package com.pds.mvc;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.lang.StringUtils;



import com.google.gson.Gson;
import com.psd.demo.common.Const;

public class Actions implements Const{
	
	
	
	   public ActionForward execute(HttpServletRequest request,  
	            HttpServletResponse response){
		   return null;
	   }
	   /**
	    * 将用户信息放入session中
	    * @param request
	    * @param obj
	    */
		protected  void setSeesionUserInfor(HttpServletRequest request,Object obj){
			request.getSession().setAttribute(Const.USER_SESSION, obj);
		}
		/**
		 * 获得系统时间
		 * @param formart "yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"
		 * @return
		 */
		protected String getSystemTimeForString(String  formart){
			SimpleDateFormat format = new SimpleDateFormat(formart);
			Date date = new Date();
			return format.format(date);
		}
		
		
		/**
		 * String for Integer
		 * @param params
		 * @return
		 */
		protected Integer getParameterStringForInteger(String params,HttpServletRequest request){
			if(StringUtils.isBlank(request.getParameter(params)))
				return 1;
			return Integer.parseInt(request.getParameter(params));
		}
		
		
		
		
		
		/**
		 * 动态赋值
		 * @param o
		 * @param request
		 */
		public void binder(Object o, HttpServletRequest request){
			try {
				BeanUtilsBean.getInstance().copyProperties(o, request);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		protected String ObjectToJsonString(Object o){
			Gson json = new Gson();
			return json.toJson(o).toString();
		} 
		
		
		protected void setRequestPage(String key,Object value,HttpServletRequest request){
			request.setAttribute(key, value);
		}
		
		
		
		/**
		 * 讲JSON数据返回到页面
		 * @param response
		 * @param jsonStr
		 */
		protected void outPrintJson(HttpServletResponse response,String jsonStr){
			try {
				PrintWriter out = response.getWriter();
				out.print(jsonStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
}
